# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# ZhihuiJin <zhihuijin@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# Fri Dec  8 14:46:39 2006
#
#----------------------------------------------------------------------------#

""" An interface to Han word list.
"""

#----------------------------------------------------------------------------#

import os
import re
import edict
from www import settings
from tools.common import sopen
from dict import hanziTable
import hanChar
import util

#----------------------------------------------------------------------------#

_wordListDir = os.path.join(settings.DATA_DIR, 'lists', 'word')

ch_word_list = os.path.join(_wordListDir, 'ch_word_list_cedict')
jp_word_list = os.path.join(_wordListDir, 'jp_word_list_edict')

#----------------------------------------------------------------------------#
def filterByLength(wordList, max, min=1):
    """ filter the word list by length
    """
    newList = []
    for word in wordList:
        length = len(word)
        if  length <= max and length >= min:
            newList.append(word)

    return newList

#----------------------------------------------------------------------------#
def filterByCharset(wordList, charList):
    """ filter the wordList by using charList
        only keep the word consisted of the character in charList
    """
    charSet = set(charList)

    newList = []
    for word in wordList:
        keep = 1
        for char in word:
            if char not in charSet: 
                keep = 0
                break
        if keep :
            newList.append(word)

    return newList

#----------------------------------------------------------------------------#
def reverseFilterByCharset(wordList, charList):
    """ filter the wordList by using charList
        delete the word with any character in charList
    """
    charSet = set(charList)

    newList = []
    for word in wordList:
        keep = 1
        for char in word:
            if char in charSet:
                keep = 0
                break
        if keep :
            newList.append(word)

    return newList


#----------------------------------------------------------------------------#

def getChHanWordList():
    """ get Chinese word only consisted of characters in GB2312
    """
    GB2312 = hanChar.getGB2312()
    wordList = [ line.rstrip() for line in sopen(ch_word_list) ]
    result = filterByCharset(wordList, GB2312)
    return result

#----------------------------------------------------------------------------#

def getJpHanWordList():
    """ get Japanese word only consisted of characters in SJIS
    """
    SJIS = hanChar.getSJIS()
    wordList = [line.rstrip() for line in sopen(jp_word_list)]
    result = filterByCharset(wordList, SJIS)
    return result

#----------------------------------------------------------------------------#

def getChShareCharWord():
    """ get Chinese word only consisted of characters in shared charset
    """
    wordList = getChHanWordList()
    shareCharSet = hanChar.getSharedCommonChar()
    result = filterByCharset(wordList, shareCharSet)
    return result

#----------------------------------------------------------------------------#
def getJpShareCharWord():
    """ get Japanese word only consisted of characters in shared charset
    """
    wordList = getJpHanWordList()
    shareCharSet = hanChar.getSharedCommonChar()
    result = filterByCharset(wordList, shareCharSet)
    return result

#----------------------------------------------------------------------------#
def convWord(word, convTable):
    """ convert word by using conversion table
        return the list of converted word
    """
    result = []
    for char in word:
        if convTable.get(char) is None:
            #raise Exception("Error: %s not in conversion table" %(char))
            convChar = [char]
        else:
            convChar = convTable[char]
             
        result = util.prodList(result, convChar)

    return result 

#----------------------------------------------------------------------------#
def getChWordConvList():
    """ convert Chinese word to Japanese word by cognate mapping
    """
    hanziToKanjiMap = hanziTable.getCognateRelation().reverseMap()

    wordList  = getChHanWordList()

    convList  = []
    for word in wordList:
        convList += convWord(word, hanziToKanjiMap)

    return convList

#----------------------------------------------------------------------------#
def getJpWordConvList():
    """ convert Chinese word to Japanese word by cognate mapping
    """
    hanziToKanjiMap = hanziTable.getCognateRelation().forwardMap()
    wordList  = getJpHanWordList()

    convList  = []
    for word in wordList:
        convList += convWord(word, hanziToKanjiMap)

    return convList

#----------------------------------------------------------------------------#
def charPercentInWord(charList, wordList):
    """ calculate the how many tokens in wordList are from charList,
        return the percentage
    """
    totalCount = 0
    charCount  = 0
    charSet    = set(charList)

    for word in wordList:
        totalCount += len(word)
        for char in word:
            if char in charSet:
                charCount += 1

    if charCount == 0:
        return 0.0
    else :
        return charCount * 1.0 / totalCount

#----------------------------------------------------------------------------#

